package com.demo.cms.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.cms.dao.ArticleDao;
import com.demo.cms.model.Article;
import com.demo.core.cache.CacheKey;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.support.GeneralExecuteModel;
import com.demo.core.model.support.SimpleBaseModel;

public class ArticleDaoImpl extends PageCoreDaoSupport<Article, Long> implements ArticleDao {

	/**
	 * 目录空间 
	 */
	protected final static String ARTICLE_SPACE = "articleSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CMS_ARTICLE";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int updateEnabled(Article article, Map<String, Object> params) {
		
		cacheClear();
		
		params.put("article", article);
		return getSqlMapClientTemplate().update(nameSpace() + "changeEnabled", params);
	}

	@Override
	protected String nameSpace() {
		return ARTICLE_SPACE;
	}

	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}

	@Override
	public int updateFolderId(Long folderId, Map<String, Object> params) {
		
		cacheClear();
		
		params.put("folderId", folderId);
		return getSqlMapClientTemplate().update(nameSpace() + "changeFolderId", params);
	}

	@Override
	public Article simpleFind(Long id) {
		Map<String, Object> params = new HashMap<String, Object>(4);
		params.put("id", id);
		List<Article> articleList = simpleFindList(params);
		return articleList.size() == 0 ? null : articleList.get(0);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Article> simpleFindList(Map<String, Object> params) {
		
		String query = "shortSelect";
		List<Article> result = null;
		CacheKey key  = null;
		if (isUseCache() && getCache() != null) {
			key = new CacheKey();
			key.update(nameSpace() + query);
			key.update(params.hashCode());
			Object obj = getCache().get(key);
			
			if (LOG.isDebugEnabled()) {
				LOG.debug(" using cache :" + key.toString() + " result:" + (obj == null ? " is null" : obj.toString()));
			}
			if (obj instanceof List) {
				// 缓存对象为List
				result = (List<Article>) obj;
			} else if (obj instanceof SimpleBaseModel) {
				// 缓存对象为单ID主键对象
				result = new ArrayList(Arrays.asList(obj));
			} else {
				// 其他类型,清除缓存
				getCache().clear(key);
				result = null;
			}
		}
		if (result == null) {
			result = getSqlMapClientTemplate().queryForList(nameSpace() + query, params);
			if (isUseCache() && getCache() != null && result != null ) {
				getCache().put(key, result);
			}
		}
		return result;
		//return getSqlMapClientTemplate().queryForList(nameSpace() + "shortSelect", params);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Article> queryArticle(Map<String, Object> params) {
		
		String query = "queryArticle";
		List<Article> result = null;
		CacheKey key  = null;
		if (isUseCache() && getCache() != null) {
			key = new CacheKey();
			key.update(nameSpace() + query);
			key.update(params.hashCode());
			Object obj = getCache().get(key);
			
			if (LOG.isDebugEnabled()) {
				LOG.debug(" using cache :" + key.toString() + " result:" + (obj == null ? " is null" : obj.toString()));
			}
			if (obj instanceof List) {
				// 缓存对象为List
				result = (List<Article>) obj;
			} else if (obj instanceof SimpleBaseModel) {
				// 缓存对象为单ID主键对象
				result = new ArrayList(Arrays.asList(obj));
			} else {
				// 其他类型,清除缓存
				getCache().clear(key);
				result = null;
			}
		}
		if (result == null) {
			result = getSqlMapClientTemplate().queryForList(nameSpace() + query, params);
			if (isUseCache() && getCache() != null && result != null ) {
				getCache().put(key, result);
			}
		}
		//return getSqlMapClientTemplate().queryForList(nameSpace() + "queryArticle", params);
		return result;
	}
}
